import {NextFunction, Request, Response} from "express";
import ClassService from "@/services/ClassService";
import ClasssModel from "@/models/ClassModel";
import {send} from "@/utils/responseUtils";

/**
 * 班级
 */
export default class ClasssController {
    /**
     * 更新班级
     * @param req
     * @param res
     */
    public async updateClass(req: Request, res: Response){
        const body=req.body
        console.log("body",body)
        if (!body.id){
            return res.send("班级id不能为空")
        }
        const data=await ClassService.update(body.id,body)
        console.log("data",data)
        send(res,{code:200,data})
    }
    /**
     * 获取班级
     * @param req
     * @param res
     */
    public getClass(req: Request, res: Response) {
        console.log("req",req.query)
        ClassService.get({id:req.query.id as string}).then(Classs => {
            res.send(Classs)
        }).catch(error => {
            res.send(error)
        })
    }

    /**
     * 添加班级
     * @param request
     * @param response
     */
    public async createClass(request: Request, response: Response) {
        console.log("createClass=================request.body",request.body)
        ClassService.create(request.body).then(res => {
            console.log("createClass=================res",res)
            response.send(res)
        })
            .catch(error => {
                console.log("createClass=================error",error)
                response.send(error.message)
            })
    }

    /**
     * 删除班级
     * @param request
     * @param response
     */
    public async deleteClass(request: Request, response: Response){
        console.log("request",request)
        const Class=await ClassService.delete(request.body.id)

        send(response,{code:200,message:Class?"删除成功":"删除失败"})
    }

}